package com.ynkbny.tasks;

import com.ynkbny.aspect.TaskOperationLog;
import com.ynkbny.domain.Enterprise;
import com.ynkbny.service.BaseJob;
import com.ynkbny.service.EnterpriseService;
import com.ynkbny.service.TEpUploadTimeService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author YY
 * @Date 2023/7/17 20:28
 */
@Component
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@ConditionalOnProperty(name = "schedule.enabled", havingValue = "false")
public class UpdateToken  implements BaseJob {

    private static final Logger log = LoggerFactory.getLogger(UpdateToken.class);

    @Resource
    private TEpUploadTimeService uploadTimeService;

    @Resource
    private EnterpriseService enterpriseService;

    /**
     * 更新所有企业的token
     */
    public void updateToken(){
        //取所有企业信息
        List<Enterprise> list = enterpriseService.list();
        for (Enterprise temp : list) {
            boolean updateToken = uploadTimeService.updateToken(temp.getEnterpriseCode(), temp.getPassword());
            log.info("{}的token更新是否成功：{}", temp.getCmpName(),updateToken);
        }
    }

    @Override
    @TaskOperationLog(description = "更新所有企业的token")
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        this.updateToken();
    }
}
